﻿using SD.Entity;
using SD.Entity.PageSearch;
using SD.Entity.SearchEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SD.DataAccess.Repository
{
    public class ActionInfoRepository : BaseRepository
    {
        //根据编号/主键得到一个实体
        public ActionInfo GetActionInfo(int Id)
        {
            string sql = @"SELECT * 
                      FROM [ActionInfo]
                      WHERE Id=@Id";
            return base.Get<ActionInfo>(sql, new { Id = Id });
        }

        //删除一条数据
        public int DeleteActionInfo(int Id)
        {
            string sql = "delete from ActionInfo where Id=@Id";
            return base.Excute(sql, new { Id = Id });
        }

        //获取List数据集合
        public List<ActionInfo> GetActionInfoList(ActionInfoSearch search)
        {
            string sql = @"SELECT *
                      FROM [ActionInfo] ";
            sql += GetstrWhere(search);
            return base.Query<ActionInfo>(sql, null);
        }

        //获取PagedList数据
        public PagedList<ActionInfo> QueryActionInfoList(ActionInfoSearch search, PageView view)
        {
            string cols = @" *";
            string strWhere = GetstrWhere(search);
            return base.PageGet<ActionInfo>(view, cols, "[ActionInfo]", strWhere, "Id", null);
        }

        //查询用户的菜单/功能权限
        public List<ActionInfo> QueryActionListByUserId(int userId, int actionTypeEnum)
        {
            string sql = @"select * from ActionInfo where Id in (
	                              select ActionInfo_Id from ActionInfoRoleInfo 
	                              where RoleInfo_Id in (
			                            select RoleInfo_Id from UserInfoRoleInfo where UserInfo_Id=@UserInfoId
			                      )
	                       ) and ActionTypeEnum=@ActionTypeEnum and layer>0";
            return base.Query<ActionInfo>(sql, new { UserInfoId = userId, ActionTypeEnum = actionTypeEnum });
        }

        //查询判断用户是否有访问该方法的权限
        public int SelectUserActionCount(int userId, string controlName, string actionName, string method)
        {
            string sql = @"select COUNT(*) from ActionInfo
                           where Id in (select ActionInfo_Id from ActionInfoRoleInfo 
	                              where RoleInfo_Id in (
			                            select RoleInfo_Id from UserInfoRoleInfo where UserInfo_Id=@UserId
			                      )) and HttpMethod=@HttpMethod and ControllerName=@ControllerName and ActionMethodName=@ActionMethodName ";
            return base.Get<int>(sql, new { UserId = userId, HttpMethod = method, ControllerName = controlName, ActionMethodName = actionName });
        }

        public string GetstrWhere(ActionInfoSearch search)
        {
            string strWhere = "";
            if (!String.IsNullOrEmpty(search.Url))
            {
                strWhere += " and Url=" + search.Url ;
            }
            
            return strWhere;
        }
    }
}
